<!--
 @description 选择家居顾问
 @fileName EditConsultant.vue
 @author zengqiongying
 @created 2022/08/05 16:09:57
-->
<template>
  <div class="mine-container">
    <MineHeader arrow title="家居顾问" bg-color="#F5F6F7" />
    <div class="fixed-foot">
      <div class="btn-submit" @click="handleSubmit">确认</div>
    </div>
    <div>
      <List
        ref="list"
        class="pt-page"
        :finished.sync="finished"
        :reset-fun="resetData"
        :load="load"
        :refresh="load"
      >
        <div class="list-container">
          <div v-for="(item,i) in list" :key="i" class="consultant-list">
            <div class="avatar">
              {{ item.name | filterFirst }}
            </div>
            <div class="content">
              <div class="name">{{ item.name }}</div>
              <div class="account">
                账号：{{ item.phone }}
              </div>
            </div>
            <div class="check" @click="handleChecked(item)">
              <svg-icon v-if="!!item.checked" class="checked" icon-class="zhengquewancheng-line" />
              <svg-icon v-else icon-class="Raidobox-weixuan" />
            </div>
          </div>
        </div>
      </List>
    </div>
  </div>
</template>

<script>
import List from '@/components/PageList'
import { mapGetters, mapMutations } from 'vuex'
import { queryShoppingGuide } from '@/apis/order'
export default {
  name: 'EditConsultant',
  components: { List },
  filters: {
    filterFirst(value) {
      return value.substr(0, 1)
    }
  },
  data() {
    return {
      shopCode: this.$route.query.shopCode,
      list: [],
      finished: false // 列表是否加载完成
    }
  },

  computed: {
    ...mapGetters('order', ['consultant'])
  },
  watch: {
    shopCode(val) {
      this.resetPageData()
    }
  },
  created() {},
  methods: {
    ...mapMutations('order', ['updateConsultant']),
    resetPageData() {
      if (this.$refs.list) {
        this.$refs.list.resetPageData()
      }
    },
    load(page, pageSize) {
      const data = {
        page,
        pageSize,
        shopCodes: [this.shopCode]
      }
      return queryShoppingGuide(data).then(res => {
        const records = (res.data && res.data.records) || []
        if (this.consultant && this.consultant.length > 0) {
          records.forEach(v => {
            const index = this.consultant.findIndex(item => item.code === v.code)
            this.$set(v, 'checked', index !== -1)
          })
        }
        this.list.push(...records)
        if (this.list.length >= res.data.total) {
          this.finished = true
        }
      })
    },
    resetData() {
      this.list = []
    },
    // 单选变更
    handleChecked(item) {
      this.$set(item, 'checked', item.checked !== true)
    },
    handleSubmit() {
      const list = this.list.filter(v => v.checked === true)
      if (list.length === 0) {
        this.$toast('请至少选择一个家居顾问')
        return
      }
      if (list.length > 3) {
        this.$toast('最多选择3个家居顾问')
        return
      }
      const data = list.map(v => {
        return {
          id: v.id,
          name: v.name,
          code: v.code,
          phone: v.phone
        }
      })
      this.updateConsultant(data)
      this.$router.go(-1)
    }
  }
}

</script>
<style lang='less' scoped>
  .fixed-foot{
    position: fixed;
    z-index: 99;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12PX 16PX 22PX;
    height: 82PX;
    box-sizing: border-box;
    background: #F5F6F7;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .btn-submit{
      flex: 1;
      height: 48PX;
      line-height: 48PX;
      background: linear-gradient(90deg, #00D5C0 0%, #05C4C3 100%);
      border-radius: 12PX;
      color: #FFFFFF;
      font-weight: bold;
      font-size: 16PX;
      text-align: center;
      letter-spacing: 1px;
    }
  }
  .pt-page{
    padding: 16px 16px 82PX;
    min-height: calc(100vh - 44PX);
    box-sizing: border-box;
    .list-container{
      padding: 0 16px;
      background: #FFFFFF;
      border-radius: 12px;
    }
  }
  .consultant-list{
    padding:  16px 0;
    border-bottom: #EEEEF3 1px solid;
    display: flex;
    .avatar{
      width: 48px;
      height: 48px;
      box-sizing: border-box;
      border-radius: 50%;
      overflow: hidden;
      margin-right: 12px;
      font-size: 20px;
      font-weight: bold;
      color: #FFB450;
      border: 4px solid #00C8B4;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .check{
      margin-left: 10px;
      width: 48px;
      height: 48px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      color: #A8B0C5;
      font-size: 20px;
      .checked{
        color: #00C8B4;
      }
    }
    .content{
      flex: 1;
      .name{
        height: 28px;
        line-height: 28px;
        font-size: 14px;
        font-weight: bold;
        color: #343640;
      }
      .account{
        height: 18px;
        line-height: 18px;
        font-size: 12px;
        color: #B5B7C4;
      }
    }
  }

</style>
